﻿using System;
using System.ComponentModel;
using System.Windows;
using Microsoft.Phone.Marketplace;

namespace MSPToolkit.Utilities
{
    /// <summary>
    /// When added to resources allows to set trial accessability of controls directly in xaml
    /// </summary>
    public class TrialHelper : INotifyPropertyChanged
    {
        public TrialHelper()
        {
            TrialHelper.IsTrialValueChanged += new EventHandler(TrialHelper_IsTrialValueChanged);
        }

        private static bool _isAlwaysTrial = false;

        /// <summary>
        /// For debuggin purposes. When set to true, all methods works like in trial. False by default.
        /// </summary>
        public static bool IsAlwaysTrial 
        {
            get
            {
                return _isAlwaysTrial;
            }
            set
            {
                _isAlwaysTrial = value;
                if (IsTrialValueChanged != null)
                {
                    IsTrialValueChanged.Invoke(null, null);
                }
            }
        }

        private static bool? _isTrial = null;

        /// <summary>
        /// returns true when in trial mode. Check's LicenseInformation only once per execution
        /// </summary>
        public static bool IsTrial 
        {
            get
            {
                if(IsAlwaysTrial) 
                    return true;
                if(_isTrial == null) 
                    _isTrial = new LicenseInformation().IsTrial();
                return _isTrial ?? true;
            }  
        }

        /// <summary>
        /// Call this method to update trial information. 
        /// We encourage you to use it at the application start or resume
        /// </summary>
        public static void UpdateTrialStatus()
        {
            _isTrial = new LicenseInformation().IsTrial();
            if (IsTrialValueChanged != null)
            {
                IsTrialValueChanged.Invoke(null, null);
            }
        }

        /// <summary>
        /// returns Visibility.Visible if in trial
        /// </summary>
        public Visibility VisibleOnTrial
        {
            get
            {
                return IsTrial ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        /// <summary>
        /// returns Visibility.Collapsed if in trial
        /// </summary>
        public Visibility CollapsedOnTrial
        {
            get
            {
                return IsTrial ? Visibility.Collapsed : Visibility.Visible;
            }
        }

        /// <summary>
        /// returns false if in trial
        /// </summary>
        public bool DisabledOnTrial
        {
            get
            {
                return !IsTrial;
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private static event EventHandler IsTrialValueChanged;

        void TrialHelper_IsTrialValueChanged(object sender, EventArgs e)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IsTrial"));
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("VisibleOnTrial"));
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("CollapsedOnTrial"));
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("DisabledOnTrial"));
            }
        }
    }
}
